package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 套餐统计
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeal() {
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(setmealMapper.countByStatus(StatusConstant.ENABLE));
        setmealOverViewVO.setDiscontinued(setmealMapper.countByStatus(StatusConstant.DISABLE));
        return setmealOverViewVO;
    }

    /**
     * 菜品统计
     * @return
     */
    @Override
    public DishOverViewVO overviewDish() {
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(dishMapper.countByStatus(StatusConstant.ENABLE));
        dishOverViewVO.setDiscontinued(dishMapper.countByStatus(StatusConstant.DISABLE));
        return dishOverViewVO;
    }

    /**
     * 订单统计
     * @return
     */
    @Override
    public OrderOverViewVO overview() {
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        // 查询总订单数量
        orderOverViewVO.setAllOrders(orderMapper.selectCountByStatus(null));
        // 查询待派送订单数量
        orderOverViewVO.setWaitingOrders(orderMapper.selectCountByStatus(Orders.TO_BE_CONFIRMED));
        // 查询待接单数量
        orderOverViewVO.setDeliveredOrders(orderMapper.selectCountByStatus(Orders.CONFIRMED));
        // 查询已完成订单数量
        orderOverViewVO.setCompletedOrders(orderMapper.selectCountByStatus(Orders.COMPLETED));
        // 查询已取消订单数量
        orderOverViewVO.setCancelledOrders(orderMapper.selectCountByStatus(Orders.CANCELLED));
        // 返回数据
        return orderOverViewVO;
    }

    /**
     * 获取范围时间内的营业数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();
        // 获取结束日时总用户数
        Integer allUserCount = userMapper.countAfter(end);
        // 获取开始日之前的用户数
        Integer oldUserCount = userMapper.countAfter(begin);
        oldUserCount = oldUserCount == null ? 0 : oldUserCount;
        allUserCount = allUserCount == null ? 0 : allUserCount;
        // -- 新增用户数
        Integer newUserCount = allUserCount - oldUserCount;

        // 获取日期范围内订单总数
        Integer validOrderCount = orderMapper.countByStatusAndDateTime(null, begin,end);
        // -- 日期范围内完成订单数
        Integer completedOrderCount = orderMapper.countByStatusAndDateTime(Orders.COMPLETED, begin,end);
        // -- 日期范围内总营业额
        Double totalAmount = orderMapper.sumAmountByStatusAndDateTime(Orders.COMPLETED, begin,end);

        // 订单数为null则设置为0
        validOrderCount = validOrderCount == null ? 0 : validOrderCount;
        completedOrderCount = completedOrderCount == null ? 0 : completedOrderCount;
        // 订单营业额为空时，设置为0
        totalAmount = totalAmount == null ? 0 : totalAmount;

        // -- 日期范围内订单完成率 如果订单总数为0则将订单完成率设置为0.0
        Double orderCompletionRate;
        Double unitPrice;
        if (completedOrderCount == 0){
            // 设置订单完成率为0
            orderCompletionRate = 0.0;
            unitPrice = 0.0;
        }else {
            // 计算订单完成率和平均客单价
            orderCompletionRate = completedOrderCount * 1.0 / validOrderCount;
            unitPrice = totalAmount / completedOrderCount;
            // 设置订单完成率保留两位小数
            BigDecimal bdOrderCompletionRate = new BigDecimal(orderCompletionRate).setScale(2, RoundingMode.HALF_UP);
            orderCompletionRate = bdOrderCompletionRate.doubleValue();
            // 设置平均客单价保留两位小数
            BigDecimal bdUnitPrice = new BigDecimal(unitPrice).setScale(2, RoundingMode.HALF_UP);
            unitPrice = bdUnitPrice.doubleValue();
        }

        // 封装需要返回的数据
        // --> 新增用户
        businessDataVO.setNewUsers(newUserCount);
        // --> 订单完成率
        businessDataVO.setOrderCompletionRate(orderCompletionRate);
        // --> 平均客单价
        businessDataVO.setUnitPrice(unitPrice);
        // --> 营业额
        businessDataVO.setTurnover(totalAmount);
        // --> 有效订单数
        businessDataVO.setValidOrderCount(completedOrderCount);
        return businessDataVO;
    }

}
